package com.yq.tanhua.ui

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.fmt.mvi.learn.gobal.log
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import com.yq.tanhua.ui.friends.FriendsViewModel
import com.yq.tanhua.ui.friends.RequestDataStatus
import com.yq.tanhua.ui.views.LoadingFooter
import kotlinx.coroutines.flow.asStateFlow

@SuppressLint("StateFlowValueCalledInComposition")
@Composable
fun RecommendMovmentsPage(
    viewModel: FriendsViewModel
) {
//    "推荐动态页面加载啦".log()
    val recomendMovmentUiState = viewModel.recomendMovmentUiState.collectAsState()
    val movments = recomendMovmentUiState.value
    if (movments.datas == null) {
//        "推荐动态页面  数据加载啦。。。".log()
        viewModel.getRecommendMovments(true)
        Text(text = "数据加载中....")
    } else {
        val refreshState =
            rememberSwipeRefreshState( movments.status == RequestDataStatus.RefreshIng)
        SwipeRefresh(state =refreshState, onRefresh = {
            viewModel.getRecommendMovments(true)
        }) {
            LazyColumn {
                items(movments.datas.size) { index ->
                    val item = movments.datas[index]
                    MovementItemView(item)
                }
                item {
                    if (movments.status == RequestDataStatus.NoMoreData) {
                        Text(text = "暂无更多数据", Modifier.fillMaxWidth(1f))
                    } else {
                        LoadingFooter()
                        if (movments.status != RequestDataStatus.LoadMoreIng) {
                            "获取更多数据".log()
                            viewModel.getRecommendMovments(false)
                        }
                    }
                }
            }
        }

    }
}
